﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.IDao;
using Chinaer.Common.Extensions;

namespace Chinaer.Dao.EF
{
    public class MenuSubCategoryDAO : Repository<Model.Entities.Model_MenuSubCategory>, IMenuSubCategoryDAO
    {

        public override bool Add(Model.Entities.Model_MenuSubCategory entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Add(entity);
        }

        public override bool Delete(Model.Entities.Model_MenuSubCategory entity)
        {
            entity.IsModelBaseNullThrowException();
            entity.IsDelete = true;
            return base.Update(entity);
            //return base.Delete(entity);
        }
        public override bool Delete(string id)
        {
            Model.Entities.Model_MenuSubCategory menuSubCategory = GetByID(id);
            return Delete(menuSubCategory);
        }
        public override int DeleteByIDs(IList<string> listID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                var queryResult = from m in context.MenuSubCategories
                                  where listID.Contains(m.ID) && m.IsUse == true && m.IsDelete == false
                                  select m;
                foreach (var item in queryResult)
                {
                    context.Entry<Model.Entities.Model_MenuSubCategory>(item).State = System.Data.Entity.EntityState.Modified;
                    item.IsDelete = true;
                }
                return ChinaerDbContext.SaveChanges();
            }
        }
        public override bool Exists(string id)
        {
            return GetByID(id) != null ? true : false;
        }
        public override Model.Entities.Model_MenuSubCategory GetByID(string id)
        {
            return ChinaerDbContext.MenuSubCategories.FirstOrDefault(m => m.ID == id && m.IsUse == true && m.IsDelete == false);
        }
        public override IList<Model.Entities.Model_MenuSubCategory> Query(string sql, int pageIndex, out int recordCount, int pageSize, params System.Data.SqlClient.SqlParameter[] sqlParams)
        {
            return base.Query(sql, pageIndex, out recordCount, pageSize, sqlParams);
        }
        public override bool Update(Model.Entities.Model_MenuSubCategory entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Update(entity);
        }

        /// <summary>
        /// 根据菜单列别编号获取子菜单类别
        /// </summary>
        /// <param name="menuCategoryID"></param>
        /// <returns></returns>
        public List<Model.Entities.Model_MenuSubCategory> GetMenuSubCategoryByMenuCategoryID(string menuCategoryID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.MenuSubCategories.Where(s => s.CategoryID == menuCategoryID && s.IsUse == true && s.IsDelete == false).ToList();
            }
        }
        /// <summary>
        /// 根据菜单类别编号和店铺编号获取子菜单类别列表
        /// </summary>
        /// <param name="menuCategoryID"></param>
        /// <param name="dianPuID"></param>
        /// <returns></returns>
        public List<Model.Entities.Model_MenuSubCategory> GetMenuSbCategoryByCategoryIDAndDianPuID(string menuCategoryID, string dianPuID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.MenuSubCategories.Where(m => m.CategoryID == menuCategoryID && m.DianPuID == dianPuID && m.IsUse == true && m.IsDelete == false).ToList();
            }
        }
    }
}
